<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>tinydtls: Contiki</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">tinydtls
   &#160;<span id="projectnumber">0.8.2</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Contiki</div>  </div>
</div><!--header-->
<div class="contents">
<p>To use tinyDTLS as Contiki application, place the source code in the directory <code>apps/tinydtls</code> in the Contiki source tree and invoke configure with the option <code>&ndash;with-contiki</code>. This will define WITH_CONTIKI in <a class="el" href="tinydtls_8h.html" title="public tinydtls API ">tinydtls.h</a> and include <code>Makefile.contiki</code> in the main Makefile. To cross-compile for another platform you will need to set your host and build system accordingly. For example, when configuring for ARM, you would invoke </p>
<div class="fragment"><div class="line">./configure --with-contiki --build=x86_64-linux-gnu --host=arm-none-eabi </div>
</div><!-- fragment --><p> on an x86_64 linux host.</p>
<p>Then, create a Contiki project with <code>APPS</code> += tinydtls in its Makefile. A sample server could look like this (with read_from_peer() and get_psk_key() as shown above).</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;contiki.h&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="tinydtls_8h.html">tinydtls.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="dtls_8h.html">dtls.h</a>&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define UIP_IP_BUF   ((struct uip_ip_hdr *)&amp;uip_buf[UIP_LLH_LEN])</span></div>
<div class="line"><span class="preprocessor"></span><span class="preprocessor">#define UIP_UDP_BUF  ((struct uip_udp_hdr *)&amp;uip_buf[UIP_LLIPH_LEN])</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="keywordtype">int</span> send_to_peer(<span class="keyword">struct</span> <a class="code" href="structdtls__context__t.html">dtls_context_t</a> *, <a class="code" href="structsession__t.html">session_t</a> *, <a class="code" href="global_8h.html#adde6aaee8457bee49c2a92621fe22b79">uint8</a> *, <span class="keywordtype">size_t</span>);</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <span class="keyword">struct </span>uip_udp_conn *server_conn;</div>
<div class="line"><span class="keyword">static</span> <a class="code" href="structdtls__context__t.html">dtls_context_t</a> *dtls_context;</div>
<div class="line"></div>
<div class="line"><span class="keyword">static</span> <a class="code" href="structdtls__handler__t.html">dtls_handler_t</a> cb = {</div>
<div class="line">  .<a class="code" href="structdtls__handler__t.html#a161ad99d77bd3b963bcd473eb3cbefe2">write</a> = send_to_peer,</div>
<div class="line">  .read  = read_from_peer,</div>
<div class="line">  .event = NULL,</div>
<div class="line">  .get_psk_key = get_psk_key</div>
<div class="line">};</div>
<div class="line"></div>
<div class="line">PROCESS(server_process, <span class="stringliteral">&quot;DTLS server process&quot;</span>);</div>
<div class="line">AUTOSTART_PROCESSES(&amp;server_process);</div>
<div class="line"></div>
<div class="line">PROCESS_THREAD(server_process, ev, data)</div>
<div class="line">{</div>
<div class="line">  PROCESS_BEGIN();</div>
<div class="line"></div>
<div class="line">  <a class="code" href="dtls_8c.html#ac203466e393ac9030d405c8f0496e9cd">dtls_init</a>();</div>
<div class="line"></div>
<div class="line">  server_conn = udp_new(NULL, 0, NULL);</div>
<div class="line">  udp_bind(server_conn, UIP_HTONS(5684));</div>
<div class="line"></div>
<div class="line">  dtls_context = <a class="code" href="dtls_8c.html#a11f53d301448aa8f1dbcfaf77441bd9c">dtls_new_context</a>(server_conn);</div>
<div class="line">  <span class="keywordflow">if</span> (!dtls_context) {</div>
<div class="line">    <a class="code" href="debug_8h.html#a94fa8dba0861075ba507ea0417fd83c8">dtls_emerg</a>(<span class="stringliteral">&quot;cannot create context\n&quot;</span>);</div>
<div class="line">    PROCESS_EXIT();</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">  <a class="code" href="dtls_8h.html#ab0e3223c87b79e4946b19538b24634da">dtls_set_handler</a>(dtls_context, &amp;cb);</div>
<div class="line"></div>
<div class="line">  <span class="keywordflow">while</span>(1) {</div>
<div class="line">    PROCESS_WAIT_EVENT();</div>
<div class="line">    <span class="keywordflow">if</span>(ev == tcpip_event &amp;&amp; uip_newdata()) {</div>
<div class="line">      <a class="code" href="structsession__t.html">session_t</a> session;</div>
<div class="line"></div>
<div class="line">      uip_ipaddr_copy(&amp;session.<a class="code" href="structsession__t.html#a0bb4423cd29b415a4cf6c32d5d9a43e4">addr</a>, &amp;UIP_IP_BUF-&gt;srcipaddr);</div>
<div class="line">      session.port = UIP_UDP_BUF-&gt;srcport;</div>
<div class="line">      session.<a class="code" href="structsession__t.html#aed21682e9f971221d9f35f1266dea125">size</a> = <span class="keyword">sizeof</span>(session.<a class="code" href="structsession__t.html#a0bb4423cd29b415a4cf6c32d5d9a43e4">addr</a>) + <span class="keyword">sizeof</span>(session.port);</div>
<div class="line">    </div>
<div class="line">      <a class="code" href="dtls_8c.html#ae272fea7269714fe7a67d6da64961731">dtls_handle_message</a>(ctx, &amp;session, uip_appdata, uip_datalen());</div>
<div class="line">    }</div>
<div class="line">  }</div>
<div class="line"></div>
<div class="line">  PROCESS_END();</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> send_to_peer(<span class="keyword">struct</span> <a class="code" href="structdtls__context__t.html">dtls_context_t</a> *ctx, <a class="code" href="structsession__t.html">session_t</a> *session, <a class="code" href="global_8h.html#adde6aaee8457bee49c2a92621fe22b79">uint8</a> *data, <span class="keywordtype">size_t</span> len) {</div>
<div class="line">  <span class="keyword">struct </span>uip_udp_conn *conn = (<span class="keyword">struct </span>uip_udp_conn *)<a class="code" href="dtls_8h.html#a95f7eb485bb814aa308e2612681176d9">dtls_get_app_data</a>(ctx);</div>
<div class="line"></div>
<div class="line">  uip_ipaddr_copy(&amp;conn-&gt;ripaddr, &amp;session-&gt;<a class="code" href="structsession__t.html#a0bb4423cd29b415a4cf6c32d5d9a43e4">addr</a>);</div>
<div class="line">  conn-&gt;rport = session-&gt;port;</div>
<div class="line"></div>
<div class="line">  uip_udp_packet_send(conn, data, len);</div>
<div class="line"></div>
<div class="line">  memset(&amp;conn-&gt;ripaddr, 0, <span class="keyword">sizeof</span>(server_conn-&gt;ripaddr));</div>
<div class="line">  memset(&amp;conn-&gt;rport, 0, <span class="keyword">sizeof</span>(conn-&gt;rport));</div>
<div class="line"></div>
<div class="line">  <span class="keywordflow">return</span> len;</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Feb 27 2015 15:15:33 for tinydtls by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>
